package Repositorio;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class RepositorioPacientePlano {
	private Statement stmt;
	private String sqlStatement;
	public RepositorioPacientePlano() {
		stmt = Conexao.getInstance().stmt;
	}
	public void cadastrarPacientePlano(String cpf, String plano, String tipo, String carteira, Date fidelidade) throws SQLException {
		sqlStatement = "INSERT INTO possuiPacientePlano VALUES('"+cpf+"','"+plano+"','"+tipo+"','"+carteira+"','"+fidelidade+"')";
		stmt.executeUpdate(sqlStatement);
	}
	public void editarPacientePlano(String cpf, String plano, String tipo, String carteira, Date fidelidade) throws SQLException {
		sqlStatement = "UPDATE possuiPacientePlano SET Plano='"+plano+"', Tipo='"+tipo+"', CodCarteirinha='"+carteira+"', Fidelidade='"+fidelidade+"' WHERE CPF='"+cpf+"'";
		stmt.executeUpdate(sqlStatement);
	}
	public void removerPacientePlano(String cpf) throws SQLException {
		sqlStatement = "DELETE FROM possuiPacientePlano WHERE CPF='"+cpf+"'";
		stmt.executeUpdate(sqlStatement);
	}
	public String[] consultarPacientePlano(String cpf) throws SQLException {
		sqlStatement = "SELECT Plano, Tipo, CodCarteirinha, Fidelidade FROM possuiPacientePlano WHERE CPF='"+cpf+"'";
		ResultSet rs = stmt.executeQuery(sqlStatement);
		rs.next();
		String[] pacientePlano = new String[4];
		pacientePlano[0] = rs.getString("Plano");
		pacientePlano[1] = rs.getString("Tipo");
		pacientePlano[2] = rs.getString("CodCarteirinha");
		pacientePlano[3] = rs.getDate("Fidelidade").toString();
		return pacientePlano;
	}
	public String[] consultarPacientePorPlano(String plano) throws SQLException {
		ResultSet rs;
		sqlStatement = "SELECT count(*) FROM possuiPacientePlano WHERE Plano='"+plano+"'";
		rs = stmt.executeQuery(sqlStatement);
		rs.next();
		int pacLength = rs.getInt("count(*)");
		String paciente[] = new String[pacLength];
		sqlStatement = "SELECT CPF FROM possuiPacientePlano WHERE Plano='"+plano+"'";
		rs = stmt.executeQuery(sqlStatement);
		int i = 0;
		while(rs.next()) {
			paciente[i] = rs.getString("CPF");
			i++;
		}
		return paciente;
	}
	public boolean verificarPacientePlano(String cpf) throws SQLException {
		boolean ret;
		sqlStatement = "SELECT * FROM possuiPacientePlano WHERE CPF='"+cpf+"'";
		ResultSet rs = stmt.executeQuery(sqlStatement);
		if(rs.next()) {
			ret = true;
		} else {
			ret = false;
		}
		return ret;
	}
};
